package com.hkd.Dao模式.Dao.DaoImp;

import com.hkd.Dao模式.Dao.ProductDao;
import com.hkd.Dao模式.util.DataBase;
import com.hkd.Dao模式.实体类.Product;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author ：陈奕安（3048279304@qq.com）
 * @date ：Created in 2025/5/28 9:47
 * @description：
 * @modified By：
 * @version:
 */
public class ProductDaoImp extends DataBase implements ProductDao {
    @Override
    public int add(Product entity) {
        String sql="select into product values(?,?,?,?)";
        return this.executeUpdate(entity.getProductid(),entity.getCategory(),entity.getName(),entity.getDescn());

    }

    @Override
    public int update(Product entity) {
        String sql="update product set category=?,name=?,descn=? where productid=?";

        return this.executeUpdate(sql,entity.getCategory(),entity.getName(),entity.getDescn(),entity.getProductid());
    }

    @Override
    public int delete(String pid) {
        String sql="delete from product where producrid=?";

        return this.executeUpdate(sql,pid);
    }

    @Override
    public List<Product> findByCatid(String catid) {
        String sql="select * from product where category=?";
        this.rs=this.executQuery(sql,catid);
        List<Product> list=new ArrayList<Product>();
        try{
            while (rs.next()){
                Product entiy=new Product();
                entiy.setProductid(rs.getString("productid"));
                entiy.setCategory(rs.getString("category"));
                entiy.setDescn(rs.getString("descn"));
                entiy.setName(rs.getString("name"));
                list.add(entiy);
            }
        }
        catch (SQLException e){
            e.printStackTrace();
        }
        return list;
    }

    @Override
    public List<Product> findByName(String name) {
        String sql="select * from where name like ?";
        this.rs=this.executQuery(sql,name);
        List<Product> list =new ArrayList<Product>();
        try{
            Product entiy=new Product();
            entiy.setName(rs.getString("name"));;
            entiy.setCategory(rs.getString("category"));
            entiy.setDescn(rs.getString("decsn"));
            entiy.setProductid(rs.getString("productid"));
            list.add(entiy);
        }
        catch (SQLException e){
            e.printStackTrace();
        }
        return list;
    }
}